package com.cntcdt.api.db.dao;

import java.util.List;

import com.cntcdt.api.db.dao.base.AddLineUserDeviceDaoBase;
import com.cntcdt.api.db.model.AddLineUserDeviceModel;

/**
 * 这是add_line_user_device的Dao实现类
 */
public class AddLineUserDeviceDao extends AddLineUserDeviceDaoBase {
	/**
	 * AddLineUserDeviceDao对象
	 */
	private static AddLineUserDeviceDao instance = new AddLineUserDeviceDao();

	/**
	 * 默认构造方法
	 */
	private AddLineUserDeviceDao() {
	}

	/**
	 * 获得对象实例
	 */
	public static AddLineUserDeviceDao getInstance() {
		return instance;
	}

	/**
	 * 
	 * @param id
	 *            id
	 * @return 删除成功返回true,否则返回false
	 */
	public List<AddLineUserDeviceModel> findByUserId(int userId) {
		String sql = "SELECT * FROM " + TABLE + " WHERE addLineUserId = ? and opStatus = 0";
		Object[] paramObjs = { userId };
		List<AddLineUserDeviceModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}

	/**
	 * 
	 * @param id
	 *            id
	 * @return 删除成功返回true,否则返回false
	 */
	public List<AddLineUserDeviceModel> findByUserIdAndSortTime(int userId, int sortTime) {
		sortTime = sortTime * 20;
		String sql = "SELECT * FROM " + TABLE + " WHERE addLineUserId = ? and opStatus = 0 limit ?,20";
		Object[] paramObjs = { userId, sortTime };
		List<AddLineUserDeviceModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}

	/**
	 * 
	 * @param id
	 *            id
	 * @return 删除成功返回true,否则返回false
	 */
	public AddLineUserDeviceModel findByUserIdAndTerminal(int userId, String terminal) {
		String sql = "SELECT * FROM " + TABLE + " WHERE addLineUserId = ? and terminal = ? and opStatus = 0";
		Object[] paramObjs = { userId, terminal };
		List<AddLineUserDeviceModel> modelList = this.queryModelList(sql, paramObjs);
		if (modelList.size() > 0) {
			return modelList.get(0);
		}
		return null;
	}

	/**
	 * 
	 * @param id
	 *            id
	 * @return 删除成功返回true,否则返回false
	 */
	public AddLineUserDeviceModel findByGroupIdAndTerminal(int groupId, String terminal) {
		String sql = "SELECT * FROM " + TABLE
				+ " WHERE adminUserGroupId = ? and terminal = ? and addLineUserId = 0 and opStatus = 0";
		Object[] paramObjs = { groupId, terminal };
		List<AddLineUserDeviceModel> modelList = this.queryModelList(sql, paramObjs);
		if (modelList.size() > 0) {
			return modelList.get(0);
		}
		return null;
	}

	public List<AddLineUserDeviceModel> findByTerminal(String terminal) {
		String sql = "SELECT * FROM " + TABLE + " WHERE terminal = ? and opStatus = 0";
		Object[] paramObjs = { terminal };
		List<AddLineUserDeviceModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}

	public List<AddLineUserDeviceModel> findByTerminalAndType(String terminal) {
		String sql = "SELECT * FROM " + TABLE + " WHERE terminal = ? and type = 1 and opStatus = 0 ";
		Object[] paramObjs = { terminal };
		List<AddLineUserDeviceModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}

	public List<AddLineUserDeviceModel> findByTerminalAndAdminUserGroupId(String terminal, int adminUserGroupId) {
		String sql = "SELECT * FROM " + TABLE + " WHERE terminal = ? and adminUserGroupId = ? and opStatus = 0 ";
		Object[] paramObjs = { terminal, adminUserGroupId };
		List<AddLineUserDeviceModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}

	public boolean isExistRecord(String terminal) {
		String sql = "select * from " + TABLE + " where terminal = ? and addLineUserId in (101,102) and opStatus = 0";
		Object[] paramObjs = { terminal };
		List<AddLineUserDeviceModel> modelList = this.queryModelList(sql, paramObjs);
		if (modelList.size() > 0) {
			return true;
		} else {
			return false;
		}
	}
}
